<template>
  <el-dialog class="edit-password" title="员工信息" append-to-body :close-on-click-modal="closeOnclickModal"
             width="500px" top="3.8vh" :visible.sync="isDialogShow" @close="close">
    <el-form :model="dialogForm" ref="dialogForm" :size="size" label-width="100px">
      <el-form-item :rules="yh_rules({required:true})" label="员工账号" prop="staffName">
        <el-input v-model="dialogForm.staffName" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item :rules="yh_rules({required:true})" label="初始密码" prop="password">
        <el-input v-model="dialogForm.password" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item :rules="yh_rules({required:true})" label="姓名" prop="realName">
        <el-input v-model="dialogForm.realName" placeholder="请输入姓名" clearable />
      </el-form-item>
      <el-form-item :rules="yh_rules({required:true})" label="身份证号" prop="idCard">
        <el-input placeholder="请输入"
                  v-model="dialogForm.idCard">
        </el-input>
      </el-form-item>
      <el-form-item :rules="yh_rules({required:true})" label="出生日期" prop="birth">
        <el-date-picker
          v-model="dialogForm.birth"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio v-model="dialogForm.sex" label="1">男</el-radio>
        <el-radio v-model="dialogForm.sex" label="2">女</el-radio>
      </el-form-item>
      <el-form-item :rules="yh_rules({required:true, type: 'phone'})" label="手机号" prop="phone">
        <el-input v-model="dialogForm.phone" placeholder="请输入手机号" clearable />
      </el-form-item>
      <el-form-item :rules="yh_rules({required:true})" label="入职日期" prop="joinTime">
        <el-date-picker
          v-model="dialogForm.joinTime"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="工资" prop="wages">
        <el-input v-model="dialogForm.wages" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="奖金" prop="bonus">
        <el-input v-model="dialogForm.bonus" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="工作量" prop="workload">
        <el-input v-model="dialogForm.workload" placeholder="请输入工作量" clearable />
      </el-form-item>
      <el-form-item label="是否管理员">
        <el-radio v-model="dialogForm.isAdmin" :label="true">是</el-radio>
        <el-radio v-model="dialogForm.isAdmin" :label="false">否</el-radio>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="close" :size="size">取消</el-button>
      <el-button type="primary" @click="submit" :size="size">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {mapGetters} from 'vuex'
  import api from '@/utils/api.js'
  export default {
    name: 'EditEmployeeModal',
    components: {

    },
    props: {
      employeeDialogShow: {
        type: Boolean,
        default: false
      },
      propInfoData: {
        type: Object
      }
    },
    data () {
      return {
        isDialogShow: false,
        dialogForm: {
          staffName: '',
          realName: '',
          password: '123456',
          phone: '',
          birth: '',
          joinTime: '',
          idCard: '',
          wages: '',
          bonus: '',
          workload: '',
          isAdmin: false,
          sex: '1'
        },
      }
    },
    computed: {
      ...mapGetters(['size', 'closeOnclickModal']),
    },
    watch: {
      employeeDialogShow (newValue) {
        this.isDialogShow = newValue
        if (newValue && this.propInfoData.staffId) {
          Object.assign(this.dialogForm, this.propInfoData)
        }
      }
    },
    methods: {
      // 提交
      submit () {
        this.$validateSubmit('dialogForm').then(() => {
          let apiFunc
          if (this.propInfoData.staffId) {
            apiFunc = api.updateStaff
          } else {
            apiFunc = api.addStaff
          }
          apiFunc(this.dialogForm).then(res => {
            this.$message.success('操作成功')
            this.$emit('refreshData')
            this.close()
          })
        })
      },
      close () {
        this.dialogForm = {
          staffName: '',
          password: '123456',
          realName: '',
          phone: '',
          birth: '',
          joinTime: '',
          idCard: '',
          workload: '',
          wages: '',
          bonus: '',
          isAdmin: false,
          sex: '1'
        }
        this.$emit('update:employeeDialogShow', false)
      },
    }
  }
</script>
